package class202205.demo25.demo22;


import java.util.Arrays;

public class demo20220522 {

    public static double avg(int[] array){
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        return sum*(1.0) / array.length;
    }

    public static void main1(String[] args) {
        int[] array = {5,65,21,34,79,63};
        System.out.println(avg(array));
    }

   public static int key(int[] array,int key){
       for (int i = 0; i < array.length; i++) {
           if (array[i]==key){
               return i;
           }
       }
       return -1;
   }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6};
        int index = key(array,5);
        if (index == -1){
            System.out.println("数组中没有你要找的关键字！");
        }else {
            System.out.println("找到了你要的关键字，下标为:"+index);
        }
    }

    public static int two(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] > key) {
                right = mid - 1;
            } else if (array[mid] < key) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,14,5};
        Arrays.sort(array);
        System.out.println("排序之后的数组:"+Arrays.toString(array));
        int index = two(array,5);
        if (index == -1){
            System.out.println("数组中没有你要找的关键字！");
        }else {
            System.out.println("找到了你要的关键字，下标为:"+index);
        }
    }
}
